<template>
  <el-dialog
    title="新建签名证书"
    :visible="visible"
    width="800px"
    :before-close="closeForm"
    :close-on-click-modal="false"
  >
    <el-form ref="signEdit" :model="sign" :rules="rules" label-width="120px">
      <el-form-item label="名称" prop="SignName">
        <el-input v-model="sign.SignName" maxlength="50" placeholder="名称" />
      </el-form-item>
      <el-form-item label="证书加密方式" prop="EncryptionType">
        <el-select v-model="sign.EncryptionType" style="width: 240px;" placeholder="证书加密方式">
          <el-option label="【国标算法】2048位RSA" :value="1" />
          <el-option label="【国标算法】3072位RSA" :value="2" />
          <el-option label="【国密算法】SM2" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="有效时段" prop="Time">
        <el-date-picker
          v-model="sign.Time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item label="密码" prop="Pwd">
        <el-input
          v-model="sign.Pwd"
          type="password"
          maxlength="16"
          placeholder="密码"
          show-password
        />
      </el-form-item>
      <el-form-item label="重复密码" prop="RepeatPwd">
        <el-input
          v-model="sign.RepeatPwd"
          type="password"
          maxlength="16"
          placeholder="重复密码"
          show-password
        />
      </el-form-item>
      <el-form-item label="Email" prop="Email">
        <el-input v-model="sign.Email" maxlength="255" placeholder="Email" />
      </el-form-item>
      <el-form-item label="国家地区" prop="Country">
        <dictItem
          v-model="sign.Country"
          :dic-id="HrItemDic.SignCountry"
          placeholder="国家"
        />
      </el-form-item>
      <el-form-item label="所在单位" prop="Company">
        <el-input
          v-model="sign.Company"
          maxlength="100"
          placeholder="所在单位"
        />
      </el-form-item>
      <el-form-item label="所在部门" prop="Dept">
        <el-input v-model="sign.Dept" maxlength="100" placeholder="所在部门" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="reset">重置</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as empSignApi from '@/api/emp/empSign'
import { HrItemDic } from '@/config/publicDic.js'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const checkPwd = (rule, value, callback) => {
      if (value == null) {
        callback()
      } else if (value !== this.sign.Pwd) {
        callback(new Error('两次密码需输入一致!'))
      } else {
        callback()
      }
    }
    return {
      HrItemDic,
      sign: {
        Time: null,
        SignName: null,
        EncryptionType: 1,
        Pwd: null,
        Country: null,
        Dept: null,
        Company: null,
        RepeatPwd: null
      },
      rules: {
        SignName: [
          {
            required: true,
            message: '名称不能为空！',
            trigger: 'blur'
          }
        ],
        EncryptionType: [
          {
            required: true,
            message: '加密方式不能为空！',
            trigger: 'blur'
          }
        ],
        Pwd: [
          {
            required: true,
            message: '密码不能为空！',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 16,
            message: '密码长度需在6-16位之间！',
            trigger: 'blur'
          }
        ],
        RepeatPwd: [
          {
            required: true,
            message: '重复密码不能为空！',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 16,
            message: '密码长度需在6-16位之间！',
            trigger: 'blur'
          },
          {
            validator: checkPwd,
            trigger: 'blur'
          }
        ],
        Time: [
          {
            required: true,
            message: '有效时段不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    comId() {
      return this.$store.getters.curComId
    },
    company() {
      return this.$store.getters.company
    },
    user() {
      return this.$store.getters.user
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    save() {
      this.$refs['signEdit'].validate((valid) => {
        if (valid) {
          this.add()
        } else {
          return false
        }
      })
    },
    async add() {
      const data = {
        SignName: this.sign.SignName,
        Dept: this.sign.Dept,
        EncryptionType: this.sign.EncryptionType,
        Country: this.sign.Country,
        CertPwd: this.sign.Pwd,
        Company: this.sign.Company,
        Email: this.sign.Email,
        BeginTime: this.sign.Time[0],
        EndTime: this.sign.Time[1]
      }
      const id = await empSignApi.Add(data)
      this.$message({
        message: '添加成功!',
        type: 'success'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true, id)
    },
    reset() {
      this.sign.Time = null
      this.sign.SignName = this.user.name
      this.sign.Dept = this.user.unitDept
      this.sign.EncryptionType = 1
      this.sign.Country = 'CN'
      this.sign.Pwd = null
      this.sign.RepeatPwd = null
      this.sign.Company = this.company[this.comId]
    },
    closeForm() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    }
  }
}
</script>
